//
//  Utility.m
//  test_plus
//
//  Created by handpay on 11-9-27.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Utility.h"
#import "CategoryUtil.h"
#import <math.h>
#import <objc/runtime.h>
//static CGFloat screenWidth = 0;

@implementation CategoryDummy

@end

@implementation NSArray (randomized)

- (NSArray*)randomizedArray{
	NSMutableArray *results = [NSMutableArray arrayWithArray:self];
    int i = [results count];
    while(--i > 0) {
        int j = rand() % (i+1);
        [results exchangeObjectAtIndex:i withObjectAtIndex:j];
    }
    return [NSArray arrayWithArray:results];
}

@end

@implementation UIView (Extended)

- (void) removeAllSubviews
{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
}

@end

@implementation UIImage (ClacSize)

-(CGSize)realSize{
    return CGSizeMake(self.size.width / 2, self.size.height / 2);
}

// 重新设置图片大小
- (UIImage *) reSize: (CGSize) size {
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

// 根据颜色生成图片
+ (UIImage *) imageWithColor:(UIColor *)color size:(CGSize)size
{
    UIImage *img = [[UIImage alloc] init];
    UIImage* tempImg = [img maskImage:color size:size];
    [img release];
    return tempImg;
}

-(UIImage*)getSubImage:(CGRect)rect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
	
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    CFRelease(subImageRef);
    UIGraphicsEndImageContext();
    return smallImage;
}
- (UIImage *)maskImage:(UIColor *)maskColor size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextClipToMask(context, rect, self.CGImage);
    CGContextSetFillColorWithColor(context, maskColor.CGColor);
    CGContextFillRect(context, rect);
    
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return smallImage;
}

@end

@implementation MainThreadData

- (id)initData:(id)tar func:(SEL)function obj1:(NSObject*)obj1 obj2:(NSObject*)obj2 wait:(BOOL)isWait{
    self = [super init];
    if (self) {
        func = function;
        arg1 = obj1;
        arg2 = obj2;
        wait = isWait;
        target = tar;
    }
    return self;
}

-(void)doFunc{
    if ([target respondsToSelector:func]) {
        [target performSelector:func withObject:arg1 withObject:arg2];
    }
}

@end

@implementation NSString (NilOrEmpty)

+(BOOL)isNilOrEmpty:(NSString*)str{
    return (!str || [str length] == 0);
}

@end

@implementation NSObject (ReleaseByCheck)

-(void)myRelease{
	if (self) {
		[self release];
		self = nil;
	}
}

@end

@implementation NSObject (PerformSelectorOnMainThreadTwoObj)

- (void) doPerformSelecotrOnMainThread:(MainThreadData*)data{
    [data doFunc];
}


- (void) performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg1 withObject:(id)arg2 waitUntilDone:(BOOL)wait{

    MainThreadData* data = [[MainThreadData alloc] initData:self func:aSelector obj1:arg1 obj2:arg2 wait:wait];
    [self performSelectorOnMainThread:@selector(doPerformSelecotrOnMainThread:) withObject:data waitUntilDone:YES];
    [data release];
    
}

@end

@implementation UITextField (ValueChanged)

@end


@implementation UIButton (CustomerButton)
-(UIImage *)initHPimgWithImgName:(NSArray *) imgNames withImgWidth:(NSInteger) imgW{
    /*if (imgNames == NULL) {
        return NULL;
    }
    UIImage * normalBg = [Utility getImageWithFileName:[imgNames objectAtIndex:0]];
    //CGSize borderSize = CGSizeMake(ceil(normalBg.size.width/2) , ceil(normalBg.size.height/2));
    CGSize borderSize = CGSizeMake(ceil(normalBg.size.width/2), normalBg.size.height);
    CGSize sizei=CGSizeMake(imgW, borderSize.height);
    UIGraphicsBeginImageContext(sizei);  
    
    [normalBg drawInRect:CGRectMake(0, 0, borderSize.width, borderSize.height)];  
    normalBg = [Utility getImageWithFileName:[imgNames objectAtIndex:1]];;
    [normalBg drawInRect:CGRectMake(borderSize.width, 
                                    0, 
                                    sizei.width - 2 * borderSize.width, 
                                    borderSize.height)];
    
    normalBg = [Utility getImageWithFileName:[imgNames objectAtIndex:2]];
    [normalBg drawInRect:CGRectMake(sizei.width - borderSize.width, 0, borderSize.width, borderSize.height)];  
    
    normalBg = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();
    return normalBg;*/
	return nil;
}
-(void)setHandpayStyle: (EBtnStyle)btnStyle withImgWidth:(NSInteger) imgW{
    //NSArray * noramlArray;
    //NSArray * touchArray;
    /*switch (btnStyle) {
        case EBtnStyleBig:
            noramlArray = [[NSArray alloc]initWithObjects:@"BtnLeft",@"BtnCenter",@"BtnRight", nil];
            touchArray = [[NSArray alloc]initWithObjects:@"BtnLeftTouch",@"BtnCenterTouch",@"BtnRightTouch", nil];
            break;
        case EBtnStyleBigGray:
            noramlArray = [[NSArray alloc]initWithObjects:@"BigBtnBaiLeft",@"BigBtnBaiCenter",@"BigBtnBaiRight", nil];
            touchArray = [[NSArray alloc]initWithObjects:@"BtnLeftTouch",@"BtnCenterTouch",@"BtnRightTouch", nil];
            break;
        case EBtnStyleSmall:
            noramlArray = [[NSArray alloc]initWithObjects:@"SmallBtnLeft",@"SmallBtnCenter",@"SmallBtnRight", nil];
            touchArray = [[NSArray alloc]initWithObjects:@"SmallBtnLeftTouch",@"SmallBtnCenterTouch",@"SmallBtnRightTouch", nil];
            break;
        case EBtnStyleSmallGray:
            noramlArray = [[NSArray alloc]initWithObjects:@"PopupGrayBtnLeft",@"PopupGrayBtnCenter",@"PopupGrayBtnRight", nil];
            touchArray = [[NSArray alloc]initWithObjects:@"PopupBtnLeftTouch",@"PopupBtnCenterTouch",@"PopupBtnRightTouch", nil];
            break;
        case EBtnStyleCheckBox:
            [self setImage:[Utility getImageWithFileName:@"SelectionNo"] forState:UIControlStateNormal];
            [self setImage:[Utility getImageWithFileName:@"SelectionYes"] forState:UIControlStateSelected];
            [self setFrame:CGRectMake(0,0,18,18)]; 
            return;
        case EBtnStyleRadioBox:
            [self setImage:[Utility getImageWithFileName:@"RadioNo"] forState:UIControlStateNormal];
            [self setImage:[Utility getImageWithFileName:@"RadioYes"] forState:UIControlStateSelected];
            [self setFrame:CGRectMake(0,0,29,29)];
            return;
        case EBtnStyleDeleted:
            [self setImage:[Utility getImageWithFileName:@"DeleteBtn"] forState:UIControlStateNormal];
            [self setImage:[Utility getImageWithFileName:@"DeleteBtnTouch"] forState:UIControlStateHighlighted];
            [self setFrame:CGRectMake(0,0,26,26)];
            return;
        default:
            noramlArray = [[NSArray alloc]initWithObjects:@"PopupBtnLeft",@"PopupBtnCenter",@"PopupBtnRight", nil];
            touchArray = [[NSArray alloc]initWithObjects:@"PopupBtnLeftTouch",@"PopupBtnCenterTouch",@"PopupBtnRightTouch", nil];
            break;
    }
    UIImage * normalBg = [self initHPimgWithImgName:noramlArray withImgWidth:imgW];   
    if (normalBg != NULL) {
        [self setBackgroundImage:normalBg forState:UIControlStateNormal];
    }
        
    UIImage * touchBg = [self initHPimgWithImgName:touchArray withImgWidth:imgW];   
    if (touchBg != NULL) {
        [self setBackgroundImage:touchBg forState:UIControlStateHighlighted];
    }
    
    [noramlArray release];
    [touchArray release];
    
    [self setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[Utility getDefaultFont]];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; */ 
}

-(void)setRadioButton:(NSString*)normal highLight:(NSString *)highLight{
	UIImage* img = [Utility GetImageByName:normal withPath:nil];
	
	[self setImage:img forState:UIControlStateNormal];
	[self setImage:[Utility GetImageByName:highLight withPath:nil] forState:UIControlStateSelected];
	
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 
							[img realSize].width, [img realSize].height);
}
@end


static char kTableViewCellHPEditingStyleKey;
@implementation UITableViewCell (CustomEditStyle)
@dynamic hpEditingStyle;
- (void) setHpEditingStyle:(UITableViewCellHPEditingStyle)hpEditingStyle
{
    objc_setAssociatedObject(self, &kTableViewCellHPEditingStyleKey, [NSNumber numberWithInt:hpEditingStyle], OBJC_ASSOCIATION_ASSIGN);
}

- (UITableViewCellHPEditingStyle) hpEditingStyle
{
    return [((NSNumber *)objc_getAssociatedObject(self, &kTableViewCellHPEditingStyleKey)) intValue];
}

@end
